#ifndef _AUTOFREELIST_HPP
#define _AUTOFREELIST_HPP

#include <vector>
#include "boost/foreach.hpp"

using namespace std;

#define foreach BOOST_FOREACH

class AutoFreeList{
    public:
        ~AutoFreeList();
        template<typename T> T* AddDel(T *p){
            AFElement<T> el;
            el.ptr=p;
            el.freetype=0;
            List.push_back(el);
            return p;
        }
        template<typename T> T* AddFree(T *p){
            AFElement<T> el;
            el.ptr=p;
            el.freetype=1;
            List.push_back(el);
            return p;
        }
    private:
        class IAFElement{
            public:
                virtual void Free(){};
                int freetype;
        };
        
        template<typename T>
        class AFElement : public IAFElement{
            public:
                T *ptr;
                void Free(){
                    switch(freetype){
                        case 0:
                            delete ptr;
                            break;
                        case 1:
                            free(ptr);
                            break;
                    }
                }
        };
        vector<IAFElement> List;
};

#endif
